package org.umlviewer.paint;


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;

/**
 * Draws a triangluar arrow head at the end of a FigEdge
 */

public class ArrowHeadTriangle extends ArrowHead{

    private static final long serialVersionUID = -438086672038929101L;

    public ArrowHeadTriangle()
    {
        
    }
    
    public void paint(
            final Graphics g,
            final Point start, 
            final Point end, 
            final Color lineColor, 
            final Color fillColor) {
        int xFrom, xTo, yFrom, yTo;
        double denom, x, y, dx, dy, cos, sin;
        Polygon triangle;

        xFrom = start.x;
        xTo = end.x;
        yFrom = start.y;
        yTo = end.y;

        dx = (double) (xTo - xFrom);
        dy = (double) (yTo - yFrom);
        denom = dist(dx, dy);
        if (denom <= 0.01)
            return;

        cos = getHeight() / denom;
        sin = getWidth() / denom;
        x = xTo - cos * dx;
        y = yTo - cos * dy;
        int x1 = (int) (x - sin * dy);
        int y1 = (int) (y + sin * dx);
        int x2 = (int) (x + sin * dy);
        int y2 = (int) (y - sin * dx);

        triangle = new Polygon();
        triangle.addPoint(xTo, yTo);
        triangle.addPoint(x1, y1);
        triangle.addPoint(x2, y2);

        Graphics graphics = (Graphics) g;
        graphics.setColor(fillColor);
        graphics.fillPolygon(triangle);
        graphics.setColor(lineColor);
        graphics.drawPolygon(triangle);
    }     

} 